package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.FunctionUsers;
import top.jnaw.jee.platform.model.Law;
import top.jnaw.jee.platform.model.Users;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Shiros;
import top.jnaw.jee.utils.Strings;
import top.jnaw.jee.utils.wechat.Wechats;

public class LawController extends Controller {

  private static final String WECHAT_STATE = "law";
  private static final String WECHAT_APP_URL =
      Consts.DOMAIN + "wechat/index.html?data=%s";

  public void index() {

    Wechats.redirect(this, WECHAT_STATE, WECHAT_APP_URL);
  }

  public void add() {
    JSONObject req = Strings.toJSONObject(this);
    final String title = Strings.getString(req, "title", "");
    final String content = Strings.getString(req, "content", "");
    final int pid = Strings.getInt(req, "pid", Consts.DEFAULT_BAD_ID);
    JSONArray accessory = Strings.getJSONArray(req, "accessory", null);

    ModelResult result = Law.add(title, content, pid, accessory);

    renderJson(result.toJson());
  }

  public void update() {
    JSONObject req = Strings.toJSONObject(this);
    final String title = Strings.getString(req, "title", "");
    final String content = Strings.getString(req, "content", "");
    final int pid = Strings.getInt(req, "pid", Consts.DEFAULT_BAD_ID);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    JSONArray accessory = Strings.getJSONArray(req, "accessory", null);

    ModelResult result = Law
        .update(id, title, content, pid, accessory);

    renderJson(result.toJson());

  }

  public void del() {
    ModelResult result = new ModelResult(Result.kModelDeleteError);
    JSONObject req = Strings.toJSONObject(this);
    JSONArray ids = Strings.getJSONArray(req, "ids", null);
    if (null != ids && ids.size() > 0) {
      for (int i = 0; i < ids.size(); i++) {
        if (Law.delete(ids.getInteger(i))) {
          result.setResult(Result.kSuccess);
        }
      }
    }
    renderJson(result.toJson());
  }

  public void query() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    if (id > 0) {
      result = Law.query(id);
    }
    renderJson(result.toJson());
  }

  public void list() {
    JSONObject req = Strings.toJSONObject(this);
    final int page = Strings.getInt(req, "page", Consts.DEFAULT_PAGE);
    final int size = Strings.getInt(req, "size", Consts.DEFAULT_PAGE_SIZE);
    final JSONArray search = Strings.getJSONArray(req, "search", null);
    renderJson(Strings
        .formatJsonDateTime(Strings.formatPage(Law.list(page, size, search))));
  }

  public void check() {
    ModelResult result = new ModelResult(Result.kUnauthorized);
    Users user = Users.getByUsername(Shiros.getUser());
    int uid = (user != null) ? user.getId() : Consts.DEFAULT_BAD_ID;
    if (uid > 0) {
      JSONObject object = new JSONObject();
      object.put("is_auth", FunctionUsers.check(2, uid));
      result.setResult(Result.kSuccess);
      result.setObject(object);
    }
    renderJson(result.toJson());
  }
}
